package com.hairline.controller;


import com.github.pagehelper.PageHelper;
import com.hairline.bean.*;
import com.hairline.bean.bo.CreateGoodsBO;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.bean.vo.CatAndBrandVO;
import com.hairline.bean.vo.CategoryL1VO;
import com.hairline.bean.vo.GoodsDetailVO;
import com.hairline.bean.vo.GoodsListVO;
import com.hairline.service.GoodsService;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.lang.System;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    GoodsService goodsService;


    /**
     * 获取整个商品列表
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("/admin/goods/list")
    public BaseRespVo goodsList(Integer page, Integer limit, String sort, String order, String goodsSn, String name) {

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        long total = goodsService.countByExample(goodsExample);
        PageHelper.startPage(page, limit);

        if (goodsSn != null && !goodsSn.equals("")) {
            criteria.andGoodsSnLike("%" + goodsSn + "%");
        }
        if (name != null && !name.equals("")) {
            criteria.andNameLike("%" + name + "%");
        }
        goodsExample.setOrderByClause(sort + " " + order);

        List<Goods> goods = goodsService.selectGoodsbyExample(goodsExample);

        GoodsListVO goodsListVO = new GoodsListVO(goods, total);
        return BaseRespVo.ok(goodsListVO);
    }

    /**
     * 商品编辑按钮
     *
     * @param id
     * @return
     */
    @RequestMapping("/admin/goods/detail")
    public BaseRespVo goodsDetail(Integer id) {
        Goods goods = goodsService.selectById(id);

        List<GoodsAttribute> goodsAttributes = goodsService.selectAttributeBygoodsId(id);
        List<GoodsSpecification> goodsSpecifications = goodsService.selectSpecificationByGoodsId(id);
        List<GoodsProduct> goodsProducts = goodsService.selectProductByGoodsId(id);
        Category category = goodsService.selectCategoryIdByGoodsCategoryId(goods.getCategoryId());
        List<Integer> categoryIds = new ArrayList<>();
        categoryIds.add(category.getPid());
        categoryIds.add(goods.getCategoryId());
        GoodsDetailVO goodsDetailVO = new GoodsDetailVO(categoryIds, goods, goodsAttributes, goodsSpecifications, goodsProducts);
        return BaseRespVo.ok(goodsDetailVO);
    }

    /**
     * @return
     */
    @RequestMapping("/admin/goods/catAndBrand")
    public BaseRespVo catAndBrand() {
        List<CatAndBrandVO> categoryList = new ArrayList();

        //先查询到所有 的L1，然后再遍历查询所有L1对应的L2
        List<CategoryL1VO> L1List = goodsService.selectCategoryL1();

        for (int i = 0; i < L1List.size(); i++) {
            CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
            catAndBrandVO.setValue(L1List.get(i).getValue());
            catAndBrandVO.setLabel(L1List.get(i).getLabel());

            List<CategoryL1VO> children = goodsService.selectCategoryL2(L1List.get(i).getValue());
            catAndBrandVO.setChildren(children);
            categoryList.add(catAndBrandVO);
        }
        List<CategoryL1VO> brandsList = goodsService.selectBrands();

        HashMap<String, Object> map = new HashMap<>();
        map.put("brandList", brandsList);
        map.put("categoryList", categoryList);
        return BaseRespVo.ok(map);
    }

    @RequestMapping("/admin/goods/delete")
    public BaseRespVo delete(@RequestBody Goods goods) {
        goodsService.deleteByIdOnGoods(goods.getId());
        goodsService.deleteBygoodsIdOnAttribute(Integer.parseInt(goods.getGoodsSn()));
        goodsService.deleteBygoodsIdOnProduct(Integer.parseInt(goods.getGoodsSn()));
        goodsService.deleteBygoodsIdOnSpecification(Integer.parseInt(goods.getGoodsSn()));
        return BaseRespVo.ok();
    }

    @RequestMapping("/admin/goods/create")
    public BaseRespVo create(@RequestBody CreateGoodsBO createGoodsBO) {
        //返回插入值的id值
        int id = goodsService.insertSelectiveOnGoods(createGoodsBO.getGoods());
        boolean b = goodsService.insertSelectiveOnSpecificationAddId(createGoodsBO.getSpecifications(), id);
        boolean b1 = goodsService.insertSelectiveOnProductsAddId(createGoodsBO.getProducts(), id);
        boolean b2 = goodsService.insertSelectiveOnAttributeAddId(createGoodsBO.getAttributes(), id);
        if (b && b1 && b2){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();

    }

    @RequestMapping("/admin/goods/update")
    public BaseRespVo update(@RequestBody CreateGoodsBO updateGoodsBO){
        delete(updateGoodsBO.getGoods());
        create(updateGoodsBO);
        return BaseRespVo.ok();

    }
}